package cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.llm;

import cn.iocoder.yudao.module.written.exam.dal.dataobject.llm.LlmEventBase;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.llm.LlmEventTypeEnum;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.llm.MessageEndEvent;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.llm.MessageEvent;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Map;

@Schema(description = "用户APP - 大模型对话 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
public class LlmChatRespVO extends LlmEventBase {

    @Schema(description = "回答内容", example = "这是一个...")
    private String answer;

    @Schema(description = "附加数据")
    private Map<String, Object> data;

    @Schema(description = "是否完成", example = "false")
    private Boolean finished = false;

    @Schema(description = "错误信息")
    private String error;

    /**
     * 获取LlmEventBase对象
     * 用于返回标准的事件对象
     *
     * @return LlmEventBase对象
     */
    public LlmEventBase toLlmEventBase() {
        // 根据当前状态创建相应的事件对象
        if (this.finished != null && this.finished) {
            // 如果已完成，返回消息结束事件
            return createMessageEndEvent();
        } else {
            // 否则返回消息事件
            return createMessageEvent();
        }
    }

    /**
     * 创建消息事件
     */
    private LlmEventBase createMessageEvent() {
        MessageEvent messageEvent = new MessageEvent();

        // 设置基础属性
        messageEvent.setEvent(LlmEventTypeEnum.MESSAGE.getType());
        messageEvent.setConversationId(this.getConversationId());
        messageEvent.setMessageId(this.getMessageId());
        messageEvent.setCreatedAt(this.getCreatedAt());
        messageEvent.setTaskId(this.getTaskId());

        // 设置消息特有属性
        messageEvent.setId(this.getMessageId());
        messageEvent.setAnswer(this.answer);

        return messageEvent;
    }

    /**
     * 创建消息结束事件
     */
    private LlmEventBase createMessageEndEvent() {
        MessageEndEvent messageEndEvent = new MessageEndEvent();

        // 设置基础属性
        messageEndEvent.setEvent(LlmEventTypeEnum.MESSAGE_END.getType());
        messageEndEvent.setConversationId(this.getConversationId());
        messageEndEvent.setMessageId(this.getMessageId());
        messageEndEvent.setCreatedAt(this.getCreatedAt());
        messageEndEvent.setTaskId(this.getTaskId());

        // 设置消息结束特有属性
        messageEndEvent.setId(this.getMessageId());

        return messageEndEvent;
    }

    @Override
    public LlmEventTypeEnum getType() {
        // 根据完成状态返回相应的事件类型
        if (this.finished != null && this.finished) {
            return LlmEventTypeEnum.MESSAGE_END;
        } else {
            return LlmEventTypeEnum.MESSAGE;
        }
    }
} 